// pages/sousuo/sousuo.js
import request from "../../utils/request"
const router = require('../../utils/router');

Page({
  

  /**
   * 页面的初始数据
   *
   */
  data: {
    value: '', // 搜索框的值
    historyList: [], // 搜索历史列表
    zhuangjias: [],
    wenzhangs:[],
    ceshis:[],
    hudas:[],
    selectedCategory: null,
    gjc: "请输入搜索关键词",
    lishi:true,
    fenlei:true,
    search:false,
    tatal:3,
    qb:true,
    baseUrl:"http://localhost:8080/img?imgUrl=images/ZXSWT/",
    isFocus: false ,
    baseUrl1: "/pages/talk/tongyong/pyygrxx?id="
  },
  onTap: function(event) {
    const url = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadHistory()
  },
  onCancel(){
    this.setData({ value: '' });
    router.switchTab('/pages/index/index');
  },
  async onSearch(e){
    const keyword = e.detail.trim();
    console.log(keyword)
    if (keyword === '') {
      return;
    }
    this.saveHistory(keyword);
    this.setData({ value: '' }); // 清空搜索框
    // 这里可以调用搜索接口，根据 keyword 搜索数据
    try{
      const res1= await request.get("/hqy/consultant/searchByKey",{
        key:keyword,
        pageSize:3,
        pageNum:1
      })
      const res2= await request.get("/dmy/featuredArticle/searchByKey",{
        key:keyword,
        pageSize:3,
        pageNum:1
      })
      const res3= await request.get("/dmy/psychologicalQuesion/getByKey",{
        key:keyword,
        pageSize:100,
        pageNum:1
      })
      const zhuanjias = res1.data.list
      const wenzhangs = res2.data.list
      const hudas = res3.data.list
      // 更新页面数据
    if(this.data.selectedCategory===null){
      this.setData({
      zhuangjias: zhuanjias,
      wenzhangs: wenzhangs,
      hudas: hudas,
      });
    }
    if(this.data.selectedCategory==='心理专家'){
      this.setData({
        zhuangjias: zhuanjias,
      });
      this.setData({
        tatal: this.data.zhuangjias.length
      });
    }
    if(this.data.selectedCategory==='心理文章'){
      this.setData({
        wenzhangs: wenzhangs
        });
      this.setData({
        tatal: this.data.wenzhangs.length
      });
    }
    if(this.data.selectedCategory==='提问回答'){
      this.setData({
        hudas: hudas,
        });
      this.setData({
        tatal: this.data.hudas.length
      });
    }
    }catch(error){
      console.error(error); // 打印错误信息
      wx.showToast({ title: '搜索失败', icon: 'none' });
    }
    // 生成假数据
    
    

    this.setData({
      search: true,
      fenlei: false,
      lishi: false,
    });
  },
  quanbu: function(event) {
    // 通过 event.currentTarget.dataset 获取参数
    const param = event.currentTarget.dataset.param; // "value1"
    let t;
    if(param==='心理专家'){
      t=this.data.zhuangjias.length;
    }
    if(param==='心理文章'){
      t=this.data.wenzhangs.length;
    }
    if(param==='心理测试'){
      t=this.data.ceshis.length;
    }
    if(param==='提问回答'){
      t=this.data.hudas.length;
    }

    this.setData({
      selectedCategory:param,
      tatal:t,
      qb:false,
    });
  },
  saveHistory(keyword) {
    let historyList = this.data.historyList;
    const index = historyList.indexOf(keyword);
    if (index !== -1) {
      historyList.splice(index, 1); // 如果已存在，移除旧的
    }
    historyList.unshift(keyword); // 添加到数组开头
    this.setData({ historyList });
    wx.setStorageSync('searchHistory', historyList); // 保存到本地存储
  },
  loadHistory() {
    const historyList = wx.getStorageSync('searchHistory') || [];
    this.setData({ historyList:historyList });
  },
  clearHistory() {
    this.setData({ historyList: [] });
    wx.removeStorageSync('searchHistory'); // 清空本地存储的历史记录
  },
  onHistoryClick(event) {
    const keyword = event.currentTarget.dataset.keyword;
    this.setData({ 
      value: keyword,
      isFocus: true  
    }); // 将历史记录设置为搜索框的值
    // 这里可以调用搜索接口，根据 keyword 搜索数据
  },
  xinlizhuanjia() {
    this.setData({ fenlei: false });
    this.setData({ lishi: false });
    this.setData({ gjc: '请输入心理专家关键词' });
    this.setData({ 
      selectedCategory: '心理专家',
      qb: false
     });
  },
  xinliwenzhang() {
    this.setData({ fenlei: false });
    this.setData({ lishi: false });
    this.setData({ gjc: '请输入心理文章关键词' });
    this.setData({ 
      selectedCategory: '心理文章',
      qb: false
     });
  },
 xinliceshi() {
    this.setData({ fenlei: false });
    console.log(this.data.fenlei);
    this.setData({ lishi: false });
    this.setData({ gjc: '请输入心理测试关键词' });
    this.setData({ 
      selectedCategory: '心理测试',
      qb: false
     });
  },
  tiwenhuda() {
    this.setData({ fenlei: false });
    this.setData({ lishi: false });
    this.setData({ gjc: '请输入提问回答关键词' });
    this.setData({ 
      selectedCategory: '提问回答',
      qb: false
     });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})